Public Class DocenteForm

    Dim operacion_ As String
    Dim MiDocente As New DocenteClass

    Public Property operacion() As String
        Get
            Return operacion_
        End Get
        Set(ByVal value As String)
            operacion_ = value
        End Set
    End Property

    Dim indice_ As Byte
    Public Property indice() As Byte
        Get
            Return indice_
        End Get
        Set(ByVal value As Byte)
            indice_ = value
        End Set
    End Property

    Private Sub Aceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Aceptar.Click


        MiDocente.Apellidos = TextBox2.Text
        MiDocente.Nombres = TextBox3.Text
        MiDocente.correo = TextBox4.Text

        Select Case operacion_
            Case "Alta"
                docentes_list.Add(MiDocente)
            Case "Elimina"
                docentes_list.RemoveAt(indice_)
            Case "Modifica"

                docentes_list.Item(indice_).Apellidos = MiDocente.Apellidos
                docentes_list.Item(indice_).Nombres = MiDocente.Nombres
                docentes_list.Item(indice_).Correo = MiDocente.Correo

                DocentesGrid.DataGridView1.Refresh()

        End Select

        Me.Close()
    End Sub

    Private Sub Cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancelar.Click
        Me.Close()
    End Sub


    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If Char.IsLetter(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True

        End If
    End Sub

    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        If Char.IsLetter(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub


    Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress

        Dim a As Integer

        'validaciones del correo
        Select Case e.KeyChar

            Case Chr(8)
                e.Handled = False

            Case "@" 'permite el ingreso de una sola arroba
                a = TextBox4.Text.IndexOf("@")

                If a > -1 Then
                    e.Handled = True
                End If

            Case "0" To "9"

                e.Handled = False

            Case "a" To "z"

                e.Handled = False

            Case "_", "-"

                e.Handled = False

            Case "."
                If TextBox4.TextLength = 0 Then Exit Sub

                Dim correo, pto As String


                correo = TextBox4.Text
                pto = correo.Substring(correo.Length - 1, 1)


                If pto = "." Then
                    e.Handled = True
                End If

            Case Else
                e.Handled = True
        End Select

    End Sub

    Private Sub TextBox4_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.LostFocus

        Dim correo, cuenta, dominio As String
        Dim ult, pri As String
        Dim arroba, punto As Integer

        Dim ente(8) As String 'valida las extenciones del dominio. .COM; .ORG; .EDU; .NET; .GOV; .GOB; .MIL; .TUR; .INF

        ente(0) = ".com"
        ente(1) = ".org"
        ente(2) = ".net"
        ente(3) = ".gov"
        ente(4) = ".gob"
        ente(5) = ".mil"
        ente(6) = ".tur"
        ente(7) = ".inf"
        ente(8) = ".edu"

        'busca si hay arroba (@) y punto(.)
        correo = TextBox4.Text

        arroba = correo.IndexOf("@") 'se fija si hay arroba, indexOf es um metodo; osea realiza una accion

        If arroba = -1 Then
            MsgBox("Falta el arroba")
            TextBox4.Focus()
            Exit Sub

        End If

        cuenta = correo.Substring(0, arroba) 'el cero indica desde donde comienza, la variable (arroba) indica la cantidad

        dominio = correo.Substring(arroba + 1)

        punto = dominio.IndexOf(".") 'se fija si hay punto


        If punto = -1 Then
            MsgBox("Falta el punto")
            TextBox4.Focus()
            Exit Sub

        End If
        pri = correo.Substring(0, 1)

        ult = correo.Substring(correo.Length - 1)

        If pri = "@" Then
            MsgBox("No puede comenzar con arroba ")
            TextBox4.Focus()
            Exit Sub

        End If

        If ult = "@" Then
            MsgBox("no se puede terminar con @")
            TextBox4.Focus()
            Exit Sub

        End If


        If pri = "." Then
            MsgBox("No puede comenzar con punto ")
            TextBox4.Focus()
            Exit Sub

        End If

        If ult = "." Then
            MsgBox("no se puede terminar con punto")
            TextBox4.Focus()
            Exit Sub

            If cuenta.Length > 255 Then

                MsgBox("supera a 255 ")
                Exit Sub

            End If
            If dominio.Length > 64 Then
                MsgBox("supera los 64 ")
            End If
        End If

        Dim x, ext As Integer
        Dim encontro As Boolean

        encontro = False


        For x = 0 To ente.Length - 1

            ext = dominio.IndexOf(ente(x))

            If ext > -1 Then

                encontro = True
                Exit For
            End If
        Next

        If Not encontro Then
            MsgBox("No hay extension")
            MsgBox("Por favor ingrese correctamente la extencion")
            TextBox4.Focus()
        End If
    End Sub

    
End Class